<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" class="el-breadcrumb">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            clearable
            v-model="queryInfo.query"
            @input="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" class="seachBtn" @click="addGoods"
            >添加商品</el-button
          >
        </el-col>
      </el-row>
      <keep-alive>
        <template>
          <el-table :data="goodsList" border style="width: 100%">
            <el-table-column type="index" label="#"> </el-table-column>
            <el-table-column prop="goods_name" label="商品名称" width="600px">
            </el-table-column>
            <el-table-column prop="goods_price" label="商品价格（元）">
            </el-table-column>
            <el-table-column prop="goods_weight" label="商品重量">
            </el-table-column>
            <el-table-column label="创建时间">
              <template slot-scope="scope">
                {{ scope.row.add_time | dateFilter }}
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  size="mini"
                ></el-button>
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  @click="delGoods(scope.row)"
                ></el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </keep-alive>
      <div class="block">
        <el-pagination
          @size-change="handleGoodsSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
// import { debounce } from '../../util/debounce'
export default {
  data() {
    return {
      goodsList: [],
      queryInfo: { query: '', pagenum: 1, pagesize: 10 },
      total: 0,
      activeIndex: '0'
    }
  },
  created() {
    this.getGoodsList()
    // this.text()
  },
  methods: {
    // 获取商品列表数据
    getGoodsList() {
      this.$http
        .get('goods', {
          params: this.queryInfo
        })
        .then(res => {
          console.log(res.data)
          if (res.data.meta.status !== 200) {
            return this.$message.error('获取商品列表数据失败')
          }
          this.goodsList = res.data.data.goods
          this.total = res.data.data.total
        })
        .catch(err => {
          this.$message.error(err)
        })
    },
    // 监听商品数量的改变
    handleGoodsSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 监听页码的改变
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    async delGoods(row) {
      const rescon = await this.$confirm(
        '此操作将永久删除该商品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => {
        return err
      })
      console.log(rescon)
      if (rescon === 'confirm') {
        const { data: res } = await this.$http.delete(`goods/${row.goods_id}`)
        if (res.meta.status !== 200) {
          return this.$message.error('删除失败')
        }
        this.$message.success('删除成功')
        this.getGoodsList()
      } else if (rescon === 'cancel') {
        return this.$message.info('取消删除')
      } else {
        return this.$message.error('删除错误')
      }
    },
    addGoods() {
      this.$router.push('/goods/add')
    }
    // text() {
    //   console.log('1', this.goodsList)
    // }
  }
}
</script>

<style scoped>
.seachBtn {
  margin-left: 20px;
}
</style>
